﻿/*******************************************************************
** 文件名:	TSingleton.cs
** 版  权:	(C) 深圳冰川网络技术有限公司 2013 - Atom
** 创建人:	
** 日  期:	2016/12/19
** 版  本:	1.0
** 描  述:	单例模板
** 应  用:  	

**************************** 修改记录 ******************************
** 修改人: 
** 日  期: 
** 描  述: 
********************************************************************/

using UnityEngine;

namespace SimpleFramework
{
    public class TSingleton<T>
        where T : class , new()
    {
        static T ms_instance = null;
        public static T Instance
        {
            get
            {
                if (null == ms_instance)
                {
                    ms_instance = new T();
                }
                return ms_instance;
            }
        }
    }

    public class TSingletonX<T>
        where T : class , new()
    {
        static T ms_instace = null;
        public static void CreateSingleton()
        {
            if (null == ms_instace)
            {
                ms_instace = new T();
            }
        }

        public static void DestroySingleton()
        {
            if (ms_instace != null)
            {
                ms_instace = null;
            }
        }

        public static T Instance
        {
            get
            {
                return ms_instace;
            }
        }
    }

    public class TMonoSingleton<T> : UnityEngine.MonoBehaviour
        where T : class
    {
        static T ms_instance;
        protected virtual void Awake()
        {
            ms_instance = this as T;
        }

        protected virtual void OnDestroy()
        {
            if( ms_instance == ( this as T ) )
            {
                ms_instance = null;
            }
        }

        public static T Instance
        {
            get
            {
                return ms_instance;
            }
        }
    }

    public class TMonoSingletonX<T> : UnityEngine.MonoBehaviour
    where T : Component
    {
        static T ms_instance = null;
        public static T Instance
        {
            get
            {
                if (null == ms_instance)
                {
                    var obj = GameObject.Find("Main");
                    if (obj == null)
                    {
                        obj = new GameObject("Main");
                    }
                    DontDestroyOnLoad(obj);
                    ms_instance = obj.AddComponent<T>();
                }
                return ms_instance;
            }
        }
    }
}